package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.SelectBean;
import com.eqdd.yiqidian.bean.rebuild.EmploymentListResultBean;
import com.eqdd.yiqidian.bean.rebuild.InviteCareerListResultBean;
import com.eqdd.yiqidian.bean.rebuild.ShareConf;
import com.eqdd.yiqidian.bean.rebuild.ShareEnterConf;
import com.eqdd.yiqidian.bean.rebuild.TeacherListBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditApplyTitleBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.EditTextActivity;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.SelectSingleStaffActivity;
import com.eqdd.yiqidian.ui.select.SelectTeacherActivity;
import com.eqdd.yiqidian.ui.select.SelectTongshiActivity;
import com.eqdd.yiqidian.ui.select.SelectTwoDeepMultiTagActivity;
import com.eqdd.yiqidian.ui.share.ShareEnterActivity;
import com.eqdd.yiqidian.utils.BottomSheetUtil;
import com.eqdd.yiqidian.utils.SelectUtil;
import com.eqdd.yiqidian.utils.SplitUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.io.Serializable;
import java.util.ArrayList;

public class ApplyTrainActivity extends SlimBaseMultiEditActivity {
    private RecyclerViewCustom dataBinding;

    @ToSave
    ArrayList<InviteCareerListResultBean> resultList;
    private SelectBean selectBean;
    private Bundle bundle;
    private ArrayList<String> types;
    private String postIds;
    private BottomSheetDialog bottomSheet;
    private String guid = "";

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "培训申请");
        initTopRightText("提交", v -> {
            if (checkParams()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.APPLY_TRAIN)
                        .params("comid", AppConstent.user.getCompanyId())
                        .params("applicantGuid", AppConstent.user.getGuid())
                        .params("applicantName", AppConstent.user.getUsername())
                        .params("depid", AppConstent.user.getDepartId())
                        .params("depName", AppConstent.user.getDepartment())
                        .params("postid", AppConstent.user.getPostId())
                        .params("postName", AppConstent.user.getPost())
                        .params("trainees", setParam(1))
                        .params("theCategory", setParam(2))
                        .params("theTheme", setParam(3))
                        .params("recoDocentGuid", guid)
                        .params("recoDocentName", setParam(4))
                        .params("budgetedExpense", setParam(5))
                        .params("thedateStart", setParam(6))
                        .params("thedateEnd", setParam(7))
                        .params("theDemand", setParam(8))
                        .params("theReason", setParam(9))
                        .params("handset", setParam(11))
                        .params("qq", setParam(12))
                        .params("betrainedPostId", TextUtils.isEmpty(postIds) ? AppConstent.user.getPostId() + "" : postIds)
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave = false;
                                    clearData();
                                    setResult(Config.REFRESH);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 1:
                if (AppConstent.user.getIsAdmin() > 0) {
                    SelectUtil.selectPostFromCom(this, resultList);
                } else if (AppConstent.user.isIsleader()) {
                    SelectUtil.selectPostFromDepart(this, resultList);
                }
                break;
            case 2:
                bundle = new Bundle();
                bundle.putInt(Config.MAX_NUM, 5);
                bundle.putInt(Config.OPTION, 45);
                bundle.putString(Config.STRING_LIST, slimAdapterEx.getDataItem(p).getContent());
                startActivityForResult(SelectTwoDeepMultiTagActivity.class, bundle, Config.SELECT_TWO_DEEP_MULTI_TAG);
                break;
            case 4:
                if (bottomSheet == null) {
                    bottomSheet = BottomSheetUtil.getBottomSheet(this, "", (recyclerView, position, v) -> {
                        bottomSheet.dismiss();
                        guid="";
                        switch (position) {
                            case 0:
                                startActivityForResult(SelectTeacherActivity.class, null, Config.SELECT_TEACHER);
                                break;
                            case 1:
                                startActivityForResult(SelectSingleStaffActivity.class, null, Config.SELECT_SINGLE_STAFF);
                                break;
                            case 2:
                                SlimEditEnterBean slimEditEnterBean = (SlimEditEnterBean) slimAdapterEx.getDataItem(4);
                                Bundle bundle = new Bundle();
                                bundle.putString("content", slimEditEnterBean.getContent());
                                bundle.putString("tip", TextUtils.isEmpty(slimEditEnterBean.getTip()) ? slimEditEnterBean.getTitle() : slimEditEnterBean.getTip());
                                bundle.putString("title", slimEditEnterBean.getTitle());
                                bundle.putBoolean("canBlank", slimEditEnterBean.isCanBlank());
                                AppConstent.schemes = slimEditEnterBean.getSchemes();
                                startActivityForResult(EditTextActivity.class, bundle, p);
                                break;
                        }
                    }, "在线讲师", "企业内部", "其它");
                }
                bottomSheet.show();
                break;

        }
    }

    @Override
    protected void setEditBeans() {
        datas.add(new SlimEditApplyTitleBean());
        if (AppConstent.user.getIsAdmin() > 0 || AppConstent.user.isIsleader()) {
            datas.add(new SlimEditEnterBean("*培训对象", AppConstent.user.getPost(), 1, true, "请选择").setNeed(true));
        } else {
            datas.add(new SlimEditHintBean("*培训对象", AppConstent.user.getPost(), 1).setNeed(true));
        }
        datas.add(new SlimEditEnterBean("*培训类别", "", 2, true, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*培训主题", "", 3, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("讲师推荐", "", 4, true, "请输入"));
        datas.add(new SlimEditEnterBean("*预算费用(元)", "", 5, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true));
        datas.add(new SlimEditTimeBean("*培训开始时间", "", 6, "请输入").setType(Config.YEAR_MONTH_DAY_TYPE).setNeed(true));
        datas.add(new SlimEditTimeBean("*培训结束时间", "", 7, "请输入").setType(Config.YEAR_MONTH_DAY_TYPE).setNeed(true));
        datas.add(new SlimEditEnterBean("培训要求", "", 8, "请输入"));
        datas.add(new SlimEditEnterBean("申请理由", "", 9, "请输入"));
        datas.add(new SlimEditHintBean("审批人", "", 10));
        datas.add(new SlimEditEnterBean("*手机", "", 11, "请输入").addSchemes(StaticScheme.ChineseMobile()));
        datas.add(new SlimEditEnterBean("*qq", "", 12, "请输入"));

    }

    @Override
    protected void loadNetData() {
        OkGo.<HttpResult<String>>post(HttpConfig.BASE_URL + HttpConfig.GET_LEADER)
                .params("userGuid", user.getGuid())
                .params("companyId", user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<String>>(ApplyTrainActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<String>> response) {
                        HttpResult<String> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.getData().get(slimAdapterEx.getData().size() - 1)
                                    .setContent(TextUtils.isEmpty(httpResult.getItems()) ? "你是最高领导人" : httpResult.getItems());
                            slimAdapterEx.notifyItemChanged(slimAdapterEx.getData().size() - 1);
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.SELECT_POST_FROM_COM:
                case Config.SELECT_POST_FROM_DEPART:
                    resultList = data.getParcelableArrayListExtra(Config.LIST);
                    postIds = SplitUtil.combin(resultList, ",", "id");
                    String postStrs = SplitUtil.combin(resultList, ",", "name");
                    slimAdapterEx.getDataItem(1).setContent(postStrs);
                    slimAdapterEx.notifyItemChanged(1);
                    break;
                case Config.SHARE_ENTER:
                    ArrayList<SelectBean> selectBeans = data.getParcelableArrayListExtra(Config.SELECTED_BEANS);
                    selectBean = selectBeans.get(0);
                    slimAdapterEx.getDataItem(4).setContent(selectBean.getContent());
                    slimAdapterEx.notifyItemChanged(4);
                    break;
                case Config.SELECT_TWO_DEEP_MULTI_TAG:
                    types = data.getStringArrayListExtra(Config.STRING_LIST);
                    String tags = SplitUtil.combin(types, ",");
                    slimAdapterEx.getDataItem(2).setContent(tags);
                    slimAdapterEx.notifyItemChanged(2);
                    break;
                case Config.SELECT_TEACHER:
                    TeacherListBean teacherListBean = (TeacherListBean) data.getSerializableExtra(Config.BEAN);
                    guid = teacherListBean.getUserGuid();
                    slimAdapterEx.getDataItem(4).setContent(teacherListBean.getRealname());
                    slimAdapterEx.notifyItemChanged(4);
                    break;
            }
        } else if (requestCode == Config.SELECT_SINGLE_STAFF && resultCode == Config.INTENT_CONTENT) {
            EmploymentListResultBean staff = (EmploymentListResultBean) data.getSerializableExtra(Config.BEAN);
            guid = staff.getGuid();
            slimAdapterEx.getDataItem(4).setContent(staff.getStaffName());
            slimAdapterEx.notifyItemChanged(4);
        }else {
            super.onActivityResult(requestCode, resultCode, data);

        }
    }
}
